<?php

use think\migration\Migrator;

class ChatRole extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    protected string $name = 'chat_role';

    public function up(): void
    {
        $table = $this->table($this->name, [
            'comment'     => 'AI角色',
            'engine'      => 'InnoDB',
            'collation'   => 'utf8mb4_general_ci',
            'id'          => 'id',
            'signed'      => false,
            'primary_key' => ['id']
        ]);
        $table->addColumn('type', 'string', [
            'comment' => '角色类型', 'limit' => 30, 'null' => false,
        ])->addColumn('category_id', 'integer', [
            'comment' => '类别ID', 'limit' => 11, 'signed' => false, 'null' => false,
        ])->addColumn('name', 'string', [
            'comment' => '名称', 'limit' => 60, 'null' => false,
        ])->addColumn('icon', 'string', [
            'comment' => '图标', 'limit' => 255, 'null' => false, 'default' => '',
        ])->addColumn('intro', 'string', [
            'comment' => '简介', 'limit' => 255, 'null' => true,
        ])->addColumn('favor_ct', 'integer', [
            'comment' => '点击量', 'limit' => 11, 'signed' => false, 'null' => false, 'default' => 0,
        ])->addColumn('used_ct', 'integer', [
            'comment' => '使用次数', 'limit' => 11, 'signed' => false, 'null' => false, 'default' => 0,
        ])->addColumn('status', 'tinyinteger', [
            'comment' => '状态', 'limit' => 1, 'null' => false, 'default' => 1,
        ])->addColumn('listorder', 'integer', [
            'comment' => '排序', 'limit' => 11, 'signed' => false, 'null' => false, 'default' => 0,
        ])->addColumn('config', 'text', [
            'comment' => '配置', 'null' => false,
        ])->addColumn('create_at', 'datetime', [
            'comment' => '添加时间', 'null' => false,
        ])->addColumn('update_at', 'datetime', [
            'comment' => '更新时间', 'null' => false,
        ])->addIndex(['category_id', 'status', 'listorder'], [
            'name' => 'category', 'type' => 'index'
        ])->addIndex(['type', 'listorder'], [
            'name' => 'type', 'type' => 'index'
        ])->save();
    }

    public function down(): void
    {
        $table = $this->table($this->name);
        $table->exists() && $table->drop()->save();
    }
}
